<?php              
include "includes".DIRECTORY_SEPARATOR."start_session.php";

if ( !defined('USERNAME') || !defined('GROUP') || GROUP != "admin" )
{
	header("Location: manage_login.php");
	exit;
}

$tablou = array("corNcType" => "",
				"denumireCorNc" => "",
				"codCorNc" => "",
				 );
	
$color = array("corNcType" => "#333333",
				"denumireCorNc" => "#333333",
				"codCorNc" => "#333333" );	
$corNcVal = array("...","Cor", "Nomenclator", "Ocupatie");
								 		
$erori = "";

$tablou_erori = array("corNcType" => "Nu a fost introdus tipul<br />",
				"denumireCorNc" => "Nu a fost introdus denumirea ocupatiei<br />",
				"codCorNc" => "Nu a fost introdus codul ocupatiei<br />",
				"codCorNc1" => "Codul introdus este incorect<br />"
			 );
   
$form_get=<<<GATA
   <form method = POST action = "admin_manage_cor_nc.php">
   <table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
   <tr>
	<td><b>Tip</b></td>
	<td><select name="corNcType">
			<option >...</option>
			<option>Cor</option>
			<option>Nomenclator</option>
			<option>Ocupatie</option>
		</select>
	</td></tr>
	<tr>
		<td><b>Denumire</b></td>
		<td><input type="text" name="denumireCorNc" size="50" maxsize="150" placeholder="Exe: Inspector"/></td>
	</tr>
	<tr>
		<td><b>Cod</b></td>
		<td><input type="text" name="codCorNc" size="50" placeholder="Exe: 71411"/></td>
	</tr>
   <tr><td></td><td><input type ="submit" value = "Adauga" ></td></tr>
   <tr><td><input type="hidden"  name="addCorNc" value="true"></td></tr>
   </table>
   </form>
GATA;


if($_SERVER['REQUEST_METHOD'] == 'GET')
{
	echo $form_get;
}


if($_SERVER['REQUEST_METHOD'] == 'POST')
{
 if( isset($_POST['addCorNc']))
 {
	$ok = 1;
			
	if($_POST["corNcType"] == "...")
	{
			$erori = $erori.$tablou_erori["corNcType"];
			$ok = 0;
			$color["corNcType"] = "red";
	}
	else $tablou["corNcType"] =$_POST["corNcType"] ;
	
	if(empty($_POST["denumireCorNc"]))
	{
			$erori = $erori.$tablou_erori["denumireCorNc"];
			$ok = 0;
			$color["denumireCorNc"] = "red";
	}
	else $tablou["denumireCorNc"] =$_POST["denumireCorNc"] ;
		
	if (empty($_POST["codCorNc"])){
		$erori = $erori.$tablou_erori["codCorNc"];
		$ok = 0;
		$color["codCorNc"] = "red";
	}
	else 
		if((preg_match('/^[0-9]+([.][0-9]+)*$/',$_POST["codCorNc"])) == 0)
		{
			$erori = $erori.$tablou_erori["codCorNc1"];
			$ok = 0;		
			$color["codCorNc"] = "red";								
		}
		else $tablou["codCorNc"] =$_POST["codCorNc"] ;
				
							
	if($ok != 1)
	{
		$corNcType_options = "";
		foreach ($corNcVal as $t)
   			if( $tablou['corNcType'] == $t )
				$corNcType_options = $corNcType_options."<option selected=\"selected\" value='$t'>$t</option>";
			else
				$corNcType_options = $corNcType_options."<option value='$t'>$t</option>";
				 
	   $form_post=<<<GATA
	   <form method = POST action = "admin_manage_cor_nc.php">
	   <table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
	   <tr>
		<td><b><font color='$color[corNcType]'>Tip</b></td>
		<td><select name="corNcType">'$corNcType_options'</select>
		</td></tr>
		<tr>
			<td><b><font color='$color[denumireCorNc]'>Denumire</b></td>
			<td><input value='$tablou[denumireCorNc]'   type="text" name="denumireCorNc" size="50" maxsize="100" placeholder="Exe: Inspector"/></td>
		</tr>
		<tr>
			<td><b><font color='$color[codCorNc]'>Cod</b></td>
			<td><input value='$tablou[codCorNc]'  type="text" name="codCorNc" size="50" placeholder="Exe: 71411"/></td>
			<input  type="hidden"  name="addCorNc" value="true" />
		</tr>
	   <tr><td></td><td><input type ="submit" value = "Adauga" ></td></tr>
	   </table>
	   </form>
			  
GATA;
		echo $form_post;
		echo "<font color=\"red\">".$erori."</font>";
		}
		else  
		{
			require_once ('mysql_connect.php');
			//"Cor", "Nomenclator", "Ocupatie"
			switch ($tablou['corNcType']) {
				case "Cor":
					$u = mysqli_query($dbc,"INSERT INTO cor (denumire, cod) VALUES ('$tablou[denumireCorNc]', '$tablou[codCorNc]')");
					//echo "<br />";	
					echo "<center><h3><font color=\"#00CC00\">Ocupatia a fost adaugata cu succes ! </font></h3></center>";
					break;
				case "Nomenclator":
					$u = mysqli_query($dbc,"INSERT INTO nc (denumire, cod) VALUES ('$tablou[denumireCorNc]', '$tablou[codCorNc]')");
					//echo "<br />";	
					echo "<center><h3><font color=\"#00CC00\">Ocupatia a fost adaugata cu succes ! </font></h3></center>";
					break;
				case "Ocupatie":
					// TO DO
					break;
			}
			//$u = mysqli_query($dbc,"INSERT INTO groups (group_name) VALUES ('$tablou[nume]')");
			
				
			
			echo "<br />";	
			echo $form_get;
		}
		
}
else 
{
	echo $form_get;
}
}
					
?>


